package com.myjob.controller;

import com.myjob.entity.MemberOrder;
import com.myjob.exception.OptionException;
import com.myjob.response.Result;
import com.myjob.service.OrderService;
import com.myjob.utils.UtilValidate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Chen Yuan
 * @create 2021-01-26  16:15
 */
@RestController
@RequestMapping("/company")
public class OrderController {
	@Autowired
	private OrderService orderService;

	/**
	 * 查询会员信息
	 * @param currentPage 当前页
	 * @param pageSize 页大小
	 * @param orderId 订单编号
	 * @param companyName 企业名称
	 * @return
	 */
	@GetMapping("/orderInfo")
	public Result selectOrder(@RequestParam("currentPage")Integer currentPage,
	                          @RequestParam(value="pageSize",required = false)Integer pageSize,
	                          @RequestParam(value="orderId",required = false)Long orderId,
	                          @RequestParam(value="companyName",required = false)String companyName,
	                          @RequestParam(value="searchValue",required = false)String searchValue){
		Result result = null;
		try {
			result = orderService.selectOrder(currentPage, pageSize, orderId, companyName, searchValue);
		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("查询失败");
		}
		return result;
	}

	/**
	 * 添加订单
	 * @param memberOrder 订单信息
	 * @return 返回订单号
	 */
	@PutMapping("/orderInfo")
	public Result addOrder(@RequestBody MemberOrder memberOrder){
		Result result = null;
		try {
			if (UtilValidate.isEmpty(memberOrder)){
				return Result.failed("参数为空");
			}
			result = orderService.addOrder(memberOrder);
		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("添加失败");
		}
		return result;
	}
}
